﻿using System;
using System.Text;

namespace DataStructure
{
    public class SingleLinkedList
    {
        private Node head;
        private int size;

        public int Count
        {
            get { return size; }
        }

        public bool IsEmpty
        {
            get { return head == null; }
        }

        public SingleLinkedList()
        {
            head = null;
            size = 0;
        }

        private class Node
        {
            public int Data;
            public Node Next;

            public Node(int data)
            {
                Data = data;
                Next = null;
            }
        }

        public void Add(int data)
        {
            Node newNode = new Node(data);

            if (head == null)
            {
                head = newNode;
            }
            else
            {
                Node current = head;

                while (current.Next != null)
                {
                    current = current.Next;
                }

                current.Next = newNode;
            }

            size++;
        }

        public void Insert(int data, int index)
        {
            if (index < 0 || index >= size) throw new IndexOutOfRangeException("索引超出范围");

            Node newNode = new Node(data);
            Node current = head;

            if (index == 0)
            {
                newNode.Next = current;
                head = newNode;
            }
            else
            {
                for (int i = 0; i < index - 1; i++)
                {
                    current = current.Next;
                }

                newNode.Next = current.Next;
                current.Next = newNode;
            }

            size++;
        }

        public void Delete(int index)
        {
            if (index < 0 || index >= size) throw new IndexOutOfRangeException("索引超出界限");

            Node curent = head;

            if (index == 0)
            {
                head = head.Next;
            }
            else
            {
                for (int i = 0; i < index - 1; i++)
                {
                    curent = curent.Next;
                }

                curent.Next = curent.Next.Next;
            }

            size--;
        }

        public void Update(int data, int index)
        {
            if (index < 0 || index >= size) throw new IndexOutOfRangeException("索引超出界限");

            Node current = head;

            for (int i = 0; i < index; i++)
            {
                current = current.Next;
            }

            current.Data = data;
        }

        public int Get(int index)
        {
            if (index < 0 || index >= size) throw new IndexOutOfRangeException("索引超出界限");

            Node current = head;

            for (int i = 0; i < index; i++)
            {
                current = current.Next;
            }

            return current.Data;
        }

        public override string ToString()
        {
            Console.Write("单向链表： Head: ");
            StringBuilder sb = new StringBuilder();


            if (head == null)
            {
                sb.Append("Null");
            }
            else
            {
                Node current = head;
                sb.Append(current.Data + " > ");

                for (int i = 0; i < size - 1; i++)
                {
                    current = current.Next;
                    sb.Append(current.Data);
                    if (i != size - 2)
                    {
                        sb.Append(" > ");
                    }
                }
            }

            return sb.ToString();
        }
    }

    class Program
    {
        static void Main2(string[] args)
        {
            SingleLinkedList singleLinkedList = new SingleLinkedList();
            singleLinkedList.Add(1);
            singleLinkedList.Add(2);
            singleLinkedList.Add(3);
            singleLinkedList.Add(4);
            singleLinkedList.Add(5);
            Console.WriteLine(singleLinkedList.ToString());
            singleLinkedList.Delete(3);
            Console.WriteLine(singleLinkedList.ToString());
        }
    }
}